﻿
using System.Collections.Generic;
using System.Data;
using System;
namespace ZHDJ.MobileInterface
{
    public partial class GetMyScoreRankList : AbstractBasePage
    {
        protected override OutParam BusinessProcess<InParam, OutParam>(InParam inParam)
        {
            // 业务逻辑返回的响应对象  
            MyScoreRankOutParam result = new MyScoreRankOutParam();
            MyScoreRankInParam param = new MyScoreRankInParam();
            string logMsg = "";
            bool isFlag = true;
                 
            try
            {
                param = inParam as MyScoreRankInParam;
                if (string.IsNullOrEmpty(param.user_id) ||
                    (param.type != "1" && param.type != "2") || 
                    (param.type == "2" && string.IsNullOrEmpty(param.branch_id)))
                {
                    result.code = "0";
                    result.desc = "请求参数不正确";

                    isFlag = false;
                }

                if (isFlag)
                {
                    if (CommonUtils.IsDisableUsers(param.user_id))
                    {
                        result.code = "0";
                        result.desc = "用户状态有问题，请联系管理员";

                        isFlag = false;
                    }
                }

                if(isFlag)
                {
                    DataSet ds = InterfaceHelper.GetMemberScoreRankList(param.type, param.branch_id, "1" , 1, 999999999);
                    if (ds != null && ds.Tables.Count == 2)
                    {
                        MyScoreRank itemTemp;
                        int recordNum = 0;
                        bool flag = false; 

                        //获取个人位置
                        foreach (DataRow dr in ds.Tables[1].Rows)
                        {
                            if (param.user_id == dr["party_id"].ToString())
                            {
                                flag = true;
                                break;
                            }

                            recordNum++;
                        }

                        if (flag)
                        {
                            int beginNum = recordNum - 20;
                            int endNum = recordNum + 20;
                            if (beginNum < 0)
                                beginNum = 0;

                            if (endNum >= ds.Tables[1].Rows.Count)
                                endNum = ds.Tables[1].Rows.Count - 1;

                            for (int i = beginNum; i <= endNum; i++)
                            {
                                itemTemp = new MyScoreRank();
                                itemTemp.party_id = ds.Tables[1].Rows[i]["party_id"].ToString();
                                itemTemp.party_name = ds.Tables[1].Rows[i]["party_name"].ToString();
                                itemTemp.rank_num = ds.Tables[1].Rows[i]["rank_num"].ToString();
                                itemTemp.total_int = ds.Tables[1].Rows[i]["total_int"].ToString();
                                result.data.list.Add(itemTemp);
                            }
                        }
                    } 
                }
                logMsg = result.desc;
            }
            catch (Exception ex)
            {
                result.code = "0";
                result.desc = "操作异常，多次出现请联系管理员"; 
                logMsg = ex.Message + ":" + ex.StackTrace;
            }

            CommonUtils.InsertMobileInterfaceLog(SystemCode +"-GetMyScoreRankList", SerializeHelper.JsonSerializer<MyScoreRankInParam>(param), logMsg); 
            return result as OutParam;
        }

        protected void Page_Load(object sender, System.EventArgs e)
        {
            if (string.Compare(Request.QueryString["Safe"], "False", true) == 0)
                ProcessRequest<MyScoreRankInParam, MyScoreRankOutParam>();
            else
                SafeProcessRequest<MyScoreRankInParam, MyScoreRankOutParam>();
        }
    }

    #region 请求参数
    public class MyScoreRankInParam
    {
        /// <summary>
        /// 必填，	用户ID
        /// </summary>
        public string user_id
        { get; set; }

        /// <summary>
        /// 用户所在支部ID
        /// </summary>
        public string branch_id
        { get; set; }

        /// <summary>
        /// 必填，	类别 1：个人积分排名，2：所在支部排名
        /// </summary>
        public string type
        { get; set; }  
    }
    #endregion

    #region 结果参数
    public class MyScoreRankOutParam
    {
        public MyScoreRankOutParam()
        {
            code = "1";
            desc = "操作成功";
            data = new MyScoreRankList();
        }

        /// <summary>
        /// 返回结果 具体见接口返回对照码 1	成功 0	失败 2	请求非法 加密结果和传入的MD5字符串进行比较，如果相同则执行接口操作，否则返回1001错误响应码
        /// </summary>
        public string code
        { get; set; }

        /// <summary>
        /// 结果描述 成功和失败都需要
        /// </summary>
        public string desc
        { get; set; }

        /// <summary>
        /// 我的积分排名列表
        /// </summary>
        public MyScoreRankList data
        { get; set; }
    }

    public class MyScoreRankList
    {
        public MyScoreRankList()
        {
            //total_pages = 0;
            list = new List<MyScoreRank>();
        }

        public List<MyScoreRank> list { get; set; }

        ///// <summary>
        ///// 总页数
        ///// </summary>
        //public int total_pages
        //{ get; set; }
    }
     
    /// <summary>
    /// 我的积分排名列表
    /// </summary>
    public class MyScoreRank
    {
        /// <summary>
        ///	党员ID
        /// </summary>
        public string party_id
        { get; set; }

        /// <summary>
        ///	党员姓名
        /// </summary>
        public string party_name
        { get; set; }

        /// <summary>
        ///	总积分
        /// </summary>
        public string total_int
        { get; set; }

        /// <summary>
        ///	排名位置
        /// </summary>
        public string rank_num
        { get; set; } 
    }
    #endregion 
}